package com.woniu.controller;

import com.woniu.service.SearchBankService;
import com.woniuxy.commons.entity.QueryConditions;
import com.woniuxy.commons.enums.SubjectEnum;
import com.woniuxy.commons.enums.TestBankEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @program: bzeducation
 * @author: Xingqilin
 * @create: 2020-07-18 16:59
 * @description: 题库查询类
 */
@RestController
@RequestMapping("/search")
public class SearchBankController {

    @Resource
    SearchBankService searchBankService;

    @RequestMapping(value = "/searchById/{questionBank}/{id}", method = RequestMethod.GET)
    public Object searchById(@PathVariable String questionBank, @PathVariable Long id) {
        Map<String, Object> map = new HashMap();
        for (TestBankEnum value : TestBankEnum.values()) {
            if (value.getName().equals(questionBank)) {
                return searchBankService.findBankById(value, id);
            }
        }
        map.put("code", 500);
        map.put("msg", "题库异常，请联系管理员！");
        return map;
    }

    @RequestMapping(value = "/searchByTopic/{questionBank}/{discipline}/{topic}", method = RequestMethod.GET)
    public List searchByTopic(@PathVariable String questionBank, @PathVariable String discipline, @PathVariable String topic) {
        for (TestBankEnum testBankEnum : TestBankEnum.values()) {
            if (testBankEnum.getName().equals(questionBank)) {
                for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                    if (subjectEnum.getName().equals(discipline)) {
                        return searchBankService.findBankByTopic(subjectEnum, testBankEnum, topic);
                    }
                }
            }
        }

        List<String> list = new LinkedList();
        list.add("500");
        list.add("查询异常，请联系管理员");
        return list;
    }

    @RequestMapping(value = "/searchByParams/{questionBank}/{discipline}", method = RequestMethod.GET)
    public HashMap<Long, String> searchByParams(@PathVariable String questionBank, @PathVariable String discipline, @ModelAttribute QueryConditions queryConditions) {
        System.out.println("questionBank:--------" + questionBank + "---" + discipline);
        System.out.println("searchByParams:------" + queryConditions);
        HashMap<Long, String> QuestionMap = new HashMap<>();
        for (TestBankEnum testBankEnum : TestBankEnum.values()) {
            if (testBankEnum.getName().equals(questionBank)) {
                for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                    if (subjectEnum.getName().equals(discipline)) {
                        HashMap<Long, Object> bankByParams = searchBankService.findBankByParams(subjectEnum, testBankEnum, queryConditions);
                        bankByParams.forEach((key, value) -> {
                            QuestionMap.put(key, value.toString());
                        });
                        return QuestionMap;
                    }
                }
            }
        }


        QuestionMap.put(500L, "题库异常请联系管理员");
        return QuestionMap;
    }

}
